package fr.utbm.gl52.smartgrid.view.listview;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.lang.ref.WeakReference;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import fr.utbm.gl52.smartgrid.model.device.IDevice;
import fr.utbm.gl52.smartgrid.util.Constants;
import fr.utbm.gl52.smartgrid.util.Controllable;

public class DeviceView extends JPanel implements Controllable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2850699092373781504L;

	WeakReference<IDevice> device;

	private JLabel name;
	private JLabel type;

	private JButton deleteButton;
	private JButton editButton;

	// TODO Layout

	public DeviceView(final IDevice deviceRef) {

		this.device = new WeakReference<IDevice>(deviceRef);

		this.deleteButton = new JButton(Messages.getString("DeviceView.0")); //TODO Buttons skinned with Images //$NON-NLS-1$
		this.editButton = new JButton(Messages.getString("DeviceView.1")); //$NON-NLS-1$
		this.editButton.setActionCommand(Constants.PREFIX_REQUEST
				+ deviceRef.getID()); // Need the type in order to load the
										// correct GUI
		this.deleteButton.setActionCommand(Constants.PREFIX_DELETE
				+ deviceRef.getID());

		this.name = new JLabel(deviceRef.getName());
		this.type = new JLabel(deviceRef.getType().toString());

		this.setLayout(new GridLayout(2, 2));

		this.add(this.name);
		this.add(this.editButton);
		this.add(this.type);
		this.add(this.deleteButton);

		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		this.setMaximumSize(new Dimension(300, 30));
	}

	public void addController(ActionListener controller) {
		this.deleteButton.addActionListener(controller);
		this.editButton.addActionListener(controller);
	}

	public void update() {
		this.name.setText(this.device.get().getName());
		this.type.setText(this.device.get().getType());
	}
}